/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.vo.CardShopDto;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.activity.service.VOrderCardShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 购买卡和商铺关联表
 *
 * @author csg
 * @date 2021-05-21 14:22:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vordercardshop" )
@Api(value = "vordercardshop", tags = "购买卡和商铺关联表管理")
@Slf4j
public class VOrderCardShopController {

    private final  VOrderCardShopService vOrderCardShopService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param vOrderCardShop 购买卡和商铺关联表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('activity_vordercardshop_view')" )
    public R getVOrderCardShopPage(Page page, VOrderCardShop vOrderCardShop) {
        return R.ok(vOrderCardShopService.page(page, Wrappers.query(vOrderCardShop)));
    }


    /**
     * 通过id查询购买卡和商铺关联表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('activity_vordercardshop_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(vOrderCardShopService.getById(id));
    }

    /**
     * 新增购买卡和商铺关联表
     * @param vOrderCardShop 购买卡和商铺关联表
     * @return R
     */
    @ApiOperation(value = "新增购买卡和商铺关联表", notes = "新增购买卡和商铺关联表")
    @SysLog("新增购买卡和商铺关联表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('activity_vordercardshop_add')" )
    public R save(@RequestBody VOrderCardShop vOrderCardShop) {
        return R.ok(vOrderCardShopService.save(vOrderCardShop));
    }

    /**
     * 修改购买卡和商铺关联表
     * @param vOrderCardShop 购买卡和商铺关联表
     * @return R
     */
    @ApiOperation(value = "修改购买卡和商铺关联表", notes = "修改购买卡和商铺关联表")
    @SysLog("修改购买卡和商铺关联表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('activity_vordercardshop_edit')" )
    public R updateById(@RequestBody VOrderCardShop vOrderCardShop) {
        return R.ok(vOrderCardShopService.updateById(vOrderCardShop));
    }

    /**
     * 通过id删除购买卡和商铺关联表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除购买卡和商铺关联表", notes = "通过id删除购买卡和商铺关联表")
    @SysLog("通过id删除购买卡和商铺关联表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('activity_vordercardshop_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(vOrderCardShopService.removeById(id));
    }

	/**
	 * 获取 试用门店列表（提取）
	 * @param cardShopDto 购买卡和商铺关联表
	 * @return
	 */
	@ApiOperation(value = "获取 试用门店列表（提取）", notes = "获取 试用门店列表（提取）")
	@GetMapping("/getCardShopPage")
	public R getCardShopPage(CardShopDto cardShopDto) {
		log.info("->获取 试用门店列表（提取）", cardShopDto);
		return R.ok(vOrderCardShopService.getCardShopPage(cardShopDto));
	}


	/**
	 * PC 获取会员能使用门店列表 分页
	 * @param page 分页
	 * @param cardShopDto 购买卡和商铺关联表
	 * @return
	 */
	@ApiOperation(value = "PC 获取会员能使用门店列表 分页", notes = "PC 获取会员能使用门店列表 分页")
	@PostMapping("/getByInfoShopPage")
	public R<Page<CardShopDto>> getByInfoShopPage(@RequestBody CardShopDto cardShopDto) {
		log.info("->PC 获取会员卡能使用门店列表 分页:{}{}", cardShopDto.getPage(), cardShopDto);
		List<Long> shopIdS = new ArrayList<>();
		List<Long> shopIdS1 = cardShopDto.getShopIdList();
		Page<CardShopDto> iPage = new Page<>();
		log.info("->获取会员卡信息");
		VInfo vInfo = new VInfo().selectById(cardShopDto.getInfoId());
		log.info("->获取卡信息");
		VCard card = new VCard().selectById(vInfo.getCardId());
		if (card.getDiscountType().equals(CardConstant.DISCOUNT_TYPE_QB)){
			log.info("->获取卡关联的群组ID");
			List<VCardGroup> cardGroupList = new VCardGroup().selectList(
					Wrappers.lambdaQuery(VCardGroup.class).eq(VCardGroup::getCardId, card.getId()));
			List<Long> groupIdList = cardGroupList.stream().map(VCardGroup::getGroupId).collect(Collectors.toList());
			log.info("->根据群组ID集合 获取门店ID集合");
			List<VGroupShop> shopIdList = new VGroupShop().selectList(
					Wrappers.lambdaQuery(VGroupShop.class).in(VGroupShop::getGroupId, groupIdList));
			shopIdS = shopIdList.stream().map(VGroupShop::getShopId).collect(Collectors.toList());

		} else {
			List<VOrderCardShop> vOrderCardShops = new VOrderCardShop().selectList(Wrappers.lambdaQuery
					(VOrderCardShop.class).eq(VOrderCardShop::getOrderCardId, vInfo.getOrderCardId()));
			if (null != vOrderCardShops && vOrderCardShops.size() > 0){
				shopIdS = vOrderCardShops.stream().map(VOrderCardShop::getShopId).collect(Collectors.toList());
			}
		}

		if (null == shopIdS || shopIdS.size() == 0){
			return R.ok(null);
		}
		List<Long> longList = shopIdS.stream().filter(item -> shopIdS1.contains(item)).collect(Collectors.toList());
		if (null == longList || longList.size() == 0){
			return R.ok(null);
		}
		cardShopDto.setShopIdList(longList);
		iPage = vOrderCardShopService.getCardShopListPage(cardShopDto.getPage(), cardShopDto);
		return R.ok(iPage);
	}

}
